#!/bin/bash

#使用说明，用来提示输入参数
usage() {
	echo "Usage: bash -x 执行脚本.sh [all|base-env|pgsql-setup|pgsql-init|docker-app]"
	exit 1
}


#基础环境
base-env(){
       cd /opt/ || exit;
       wget $1;
       unzip mesx-deploy.zip;
       wget -O /etc/yum.repos.d/docker-ce.repo https://repo.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo;
       sudo sed -i 's+download.docker.com+repo.huaweicloud.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo;
       yum install docker-ce docker-compose -y;
       wget -O /etc/yum.repos.d/docker-ce.repo https://repo.huaweicloud.com/docker-ce/linux/centos/docker-ce.repo;
       sudo sed -i 's+download.docker.com+repo.huaweicloud.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo;
       yum install docker-ce docker-compose -y;
       systemctl enable docker;
       systemctl start docker;
       docker login -u cn-south-1@ZQX8P9MYV8XTCDO9IHVO -p 8da77c1f9437f372ddac224fbcacd0d110703d01effc409709780d5852ebb4d7 swr.cn-south-1.myhuaweicloud.com;
       DISK0=`lsblk -o NAME|tail -n 1`;
       mkfs.xfs /dev/$DISK0;
       echo "/dev/$DISK0  /home  xfs  defaults  0  0" >> /etc/fstab 
       mount -a;
       echo >  /tmp/tmp.log;
       chmod 666 /tmp/tmp.log;
       docker load -i /opt/mesx-deploy/nacos.tar;

}

#pg安装
pgsql-setup(){
       cd /opt/mesx-deploy/pgsql/ || exit;
       bash -x /opt/mesx-deploy/pgsql/pg_setup.sh >> /tmp/tmp.log 2>&1;
}

#pg init
pgsql-init(){
       cd /opt/mesx-deploy/pgsql/sql/ || exit;
       su  postgres -c "bash -x /opt/mesx-deploy/pgsql/sql/init-sql.sh >> /tmp/tmp.log 2>&1" ;
}

#docker部署
docker-app(){
       mkdir /opt/rocketmq-server;
       mkdir /opt/rocketmq-broker/conf -p; 
       ETH0=`hostname -I|awk '{print $1}'`;
       echo "namesrvAddr=$ETH0:9876" > /opt/rocketmq-broker/conf/broker.conf 
       echo "brokerClusterName = DefaultCluster" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "brokerName = broker-a" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "brokerId = 0" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "deleteWhen = 04" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "fileReservedTime = 48" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "brokerRole = ASYNC_MASTER" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "flushDiskType = ASYNC_FLUSH" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "brokerIP1 = $ETH0" >> /opt/rocketmq-broker/conf/broker.conf 
       echo "listenPort=10911" >> /opt/rocketmq-broker/conf/broker.conf 
       
       #启动rocketmq-server
       docker run -d \
       --restart=always \
       --name rmqnamesrv \
       -p 9876:9876 \
       -v /opt/rocketmq-server/logs:/root/logs \
       -v /opt/rocketmq-server/store:/root/store \
       -e "MAX_POSSIBLE_HEAP=100000000" \
       -e "JAVA_OPT_EXT=-server -Xms128m -Xmx1024m -Xmn128m" \
       foxiswho/rocketmq:4.7.0 \
       sh mqnamesrv;
       
       #启动rocketmq-broker
       docker run -d  \
       --restart=always \
       --name rmqbroker \
       --link rmqnamesrv:namesrv \
       -p 10911:10911 \
       -p 10909:10909 \
       -v  /opt/rocketmq-broker/logs:/root/logs \
       -v  /opt/rocketmq-broker/store:/root/store \
       -v /opt/rocketmq-broker/conf/broker.conf:/opt/rocketmq-4.7.0/conf/broker.conf \
       -e "NAMESRV_ADDR=namesrv:9876" \
       -e "MAX_POSSIBLE_HEAP=200000000" \
       -e "autoCreateTopicEnable=true" \
       -e "JAVA_OPT_EXT=-server -Xms128m -Xmx1024m -Xmn128m" \
       foxiswho/rocketmq:4.7.0 \
       sh mqbroker -c /opt/rocketmq-4.7.0/conf/broker.conf;
       
       #启动RocketMQ的管理工具rocketmq-console
       docker run -itd --restart=always -e "JAVA_OPTS=-Drocketmq.namesrv.addr=$ETH0:9876 -Dcom.rocketmq.sendMessageWithVIPChannel=false" -p 8083:8080 -t styletang/rocketmq-console-ng:latest;

}   

#docker-compose部署
docker-deploy(){
       cd  /opt/mesx-deploy/app/ || exit;
       bash -x /opt/mesx-deploy/app/deploy.sh mount >> /tmp/tmp.log 2>&1;
       sed -i "s/192.168.1.230/$ETH0/g" /opt/mesx-deploy/app/nginx/web/nginx.conf;
       bash -x /opt/mesx-deploy/app/deploy.sh base >> /tmp/tmp.log 2>&1;
       bash -x /opt/mesx-deploy/app/deploy.sh modules >> /tmp/tmp.log 2>&1;
       
       if test  -f "/opt/mesx-deploy/html.tar.gz" ;then
           mv /opt/mesx-deploy/html.tar.gz /docker/nginx/web;
           cd /docker/nginx/web || exit;
           tar -xzvf html.tar.gz;	      
	   fi
       docker restart `docker ps | grep 8000 | grep nginx | awk '{print $1}'`;
}

#根据输入参数，选择执行对应方法，不输入则执行使用说明
case "$1" in
"all")
	base-env $2
       pgsql-setup
       pgsql-init
       docker-app
       docker-deploy
       if netstat -nltp|grep -q "8000";then 
          echo "MES制造执行系统安装成功" >>  /tmp/tmp.log;
       fi
      
;;
"base-env")
	base-env $2
;;
"pgsql-setup")
	pgsql-setup
;;
"pgsql-init")
	pgsql-init
;;
"docker-app")
	docker-app
;;
"docker-deploy")
	docker-deploy
;;
*)
	usage
;;
esac